/* Team 5
 * James McQueen
 * Corwyn Simpson
 * May 1 ish, 2012
 */
package spider.controllers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.util.Enumeration;

import spider.models.Page;

/**
 * This give the description for a Retriever, responsible for obtaining
 * a link from the LinkBuffer and expanding a page.
 * @author James McQueen
 */
public class Retriever implements Runnable 
{
    /**
     * The top level domain name being passed in.
     */
    private URL my_url;
    
    /**
     * The buffer to add the fetched page to.
     */
    private Page my_page;

    /**
     * Create a Retriever given the PageBuffer, and a URL.
     * @param the_pages
     * @param the_url 
     */
    public Retriever(URL the_url) 
    {
        my_url = the_url;
    }
    
    public Retriever(final RetrieverGroup the_retriever_group)
    {
        this(the_retriever_group.getLinkBuffer().removeLink());
    }
    
    public Page getPage()
    {
        return my_page;
    }
    
    public URL getURL()
    {
        return my_url;
    }
    
    public void retrievePage() throws IOException 
    {
        //Wait for a network device to be connected to a network
        while (!pollNetworkDevices())
        {
            try
            {
                Thread.sleep(250);
            }
            catch (InterruptedException e)
            {
                return;
            }
        }
        BufferedReader input = new BufferedReader(
                new InputStreamReader(my_url.openStream()));

        StringBuilder page = new StringBuilder();
        String line_copy = input.readLine();
        while (line_copy != null)
        {
            line_copy = input.readLine();
            page.append(line_copy);
            page.append("\n");
        }
        my_page = new Page(my_url, page.toString()); 
    }
    
    /**
     * Checks to see if there is a network device that is up that isn't a 
     * loopback device (localhost, typically).
     * @return True if there is a network device that is up.
     */
    private boolean pollNetworkDevices()
    {
        boolean result = false;
        try
        {
            Enumeration<NetworkInterface> interfaces = 
                    NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements() && !result)
            {
                NetworkInterface net = interfaces.nextElement();
                result = net.isUp() && !net.isLoopback();
            }
        }
        catch (SocketException e)
        {}
        return result;
    }

    @Override
    public void run()
    {
        try 
        {
            retrievePage();
        } 
        catch (IOException ex) 
        {
            System.err.println("Invalid page: " + ex);
        }
    }
}